<?php
//检查session
function Csession(){
	$admin = session(getOnlyName("admin"));
	if($admin['sversion']!=md5(__ROOT__)){//版本号
	}elseif($admin['username'] == NULL){//用户名
	}elseif($admin['effective']<time()){
	}else{
		$admin['effective'] = time()+60*60;
		session(getOnlyName("admin"),$admin);
		return true;	
	}
	session(getOnlyName("admin"),null);//设置SESSION
	return false;
}
//获取项目唯一名称
function getOnlyName($name=""){
	if(empty($name))return false;
	return SESSION_VERSION."_".$name;
}
function getBrowser(){
	if(empty($_SERVER['HTTP_USER_AGENT'])){   //
		return '命令行，机器人来了！';
	}
	elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 9.0')){
		return 'Internet Explorer 9.0';
	}
	elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0')){
		return 'Internet Explorer 8.0';
	}
	elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0')){
		return 'Internet Explorer 7.0';
	}
	elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6.0')){
		return 'Internet Explorer 6.0';
	}
	elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')){
		return 'Firefox';
	}
	elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Chrome')){
		return 'Chrome';
	}
	elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Safari')){
		return 'Safari';
	}
	elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Opera')){
		return 'Opera';
	}
	elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'360SE')){
		return '360SE';
	}	
}
//某月的最后一天
function getLastDay($year, $month) {
    $t = mktime(0, 0, 0, $month + 1, 1, $year);
    $t = $t - 60 * 60 * 24;
    return $t;
}

//公共分页
function MyPage($total,$num=10){
	$p = new \Think\Page($total, $num);
	$p->setConfig('header', '条数据'); 
	$page = $p->show();            //分页的导航条的输出变量	 
	$res = array(
				"firstRow"=>$p->firstRow,
				"listRows"=>$p->listRows,
				"page"=>$page
		   );
	return $res;   
}

//获取编辑器
function MyHtml($name,$content){
	vendor("ckeditor.ckeditor");	
	$CKEditor= new CKEditor();   //实例化FCKeditor对象
	$CKEditor->basePath = "/tp/Library/Vendor/ckeditor/";	
	$CKEditor->config['Enabled'] = true ;
	$CKEditor->config['height'] = 300;
	$CKEditor->config['width'] = 850;
	$CKEditor->config['uiColor'] = '#f6f6f2';//背景颜色
	$CKEditor->config['toolbarLocation'] = 'top';//
	$CKEditor->config['img_dir']="/ckeditor/upload/img";		
	$CKEditor->config['toolbar'] ='Full';
	$CKEditor->config['filebrowserImageUploadUrl'] = '/admin.php?s=/Sys/ck_upload';//使用上传功能
	$CKEditor->returnOutput = true;
	$html=$CKEditor->editor($name,$content,$config);
	return $html;
}

//获取频道
function GetCnlOption($id,$showlx = false){
	import('@.Class.tree');
	$m = M("Sys_channel");
	if(!$showlx){
		$m->where("code<>'liuxue'");		
	}
	$m->field('id as cid,name as cname,code,0 as pid');
	$list = $m->select();
	if(!empty($list)){
		foreach($list as $k=>$v){
			$arr[$v['cid']] = $v;
		}
		$lists = $arr;
		
		$tree = new tree();	
		$tree->tree($lists);	
		$str = "<option value=\$cid \$selected>\$spacer\$cname</option>"; 
		$cate = $tree->get_tree(0,$str,$id); 
	}	
	return $cate;
}

//分类方法
function cateFun($model='',$pid=0){
	$Cate = M($model);		
	$list = $Cate->select();
	if(!empty($list)){
		foreach($list as $k=>$v){
			$arr[$v['cid']] = $v;
		}
		$lists = $arr;
		$tree = new \Tree();	
		$tree->tree($lists);	
		$str = "<option value=\$cid \$selected>\$spacer\$cname</option>"; 
		$cate = $tree->get_tree(0,$str,$pid); 
	}
	return $cate;			
}

function getMyUrl($name){
	preg_match("/[\w\-]+\.\w+$/", $_SERVER['HTTP_HOST'], $httpUrl);
	return "http://".$name.".".$httpUrl[0];	
}

function delDirAndFile($path, $delDir = FALSE) {
	if (is_array($path)) {
		foreach ($path as $subPath)
			delDirAndFile($subPath, $delDir);
	}
	if (is_dir($path)) {
		$handle = opendir($path);
		if ($handle) {
			while (false !== ( $item = readdir($handle) )) {
				if ($item != "." && $item != "..")
					is_dir("$path/$item") ? delDirAndFile("$path/$item", $delDir) : unlink("$path/$item");
			}
			closedir($handle);
			if ($delDir)
				return rmdir($path);
		}
	} else {
		if (file_exists($path)) {
			return unlink($path);
		} else {
			return FALSE;
		}
	}
	clearstatcache();
 }

